package com.ys.servers.common.jdbc;

import com.zaxxer.hikari.HikariDataSource;
import io.vertx.core.Vertx;
import io.vertx.core.json.JsonObject;
import io.vertx.ext.jdbc.JDBCClient;

/**
 * Created by taotao on 2017/3/8.
 */
public class JDBCHikariWrapper extends JDBCWrapper{

    @Override
    protected void initClient(Vertx vertx, JsonObject config) {
        this.client = JDBCClient.createNonShared(vertx, config);
    }

    public JDBCHikariWrapper(Vertx vertx, JsonObject config) {
        initClient(vertx, config);
       //
    }


    public HikariDataSource getHikariDataSource(JsonObject configJson){
        HikariDataSource hikariDataSource = new HikariDataSource();
        hikariDataSource.setDriverClassName(configJson.getString("jdbc.driverClassName"));
        hikariDataSource.setJdbcUrl(configJson.getString("jdbc.url"));
        hikariDataSource.setUsername(configJson.getString("jdbc.username"));
        hikariDataSource.setPassword(configJson.getString("jdbc.password"));
        hikariDataSource.setConnectionTestQuery(configJson.getString("jdbc.connectionTestQuery"));
        hikariDataSource.setConnectionTimeout(configJson.getInteger("jdbc.connectionTimeout"));
        hikariDataSource.setIdleTimeout(configJson.getInteger("jdbc.idleTimeout"));
        hikariDataSource.setMaxLifetime(configJson.getInteger("jdbc.maxLifetime"));
        hikariDataSource.setMinimumIdle(configJson.getInteger("jdbc.minimumIdle"));
        hikariDataSource.setMaximumPoolSize(configJson.getInteger("jdbc.maximumPoolSize"));
        hikariDataSource.setAutoCommit(false);
        return hikariDataSource;
    }
}
